$(function () {
    $("#workflowCurrentGrid").Grid({
        url: '../workflow/current/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '所属流程', name: 'workflowName', index: 'workflow_id', width: 80},
			{label: '所属业务', name: 'businessName', index: 'business_id', width: 80},
			{label: '审核结果', name: 'audit', index: 'audit', width: 80,
				formatter: function (value) {
					var result="";
					switch (value) {
						case 1:result='<span class="label label-info">审核中</span>';break;
						case 2:result='<span class="label label-success">通过</span>';break;
						case 3:result='<span class="label label-danger">不通过</span>';break;
						default:result='<span class="label label-success">正常</span>';break;
					}
					return result;
				}
			},
			{label: '原因', name: 'reason', index: 'reason', width: 80}]
    });
});

var vm = new Vue({
	el: '#workflowCurrent',
	data: {
        showList: true,
        title: null,
		workflowCurrent: {},
		ruleValidate: {},
		q: {
		    businessName: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		update: function (event) {
            var id = getSelectedRow("#workflowCurrentGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.workflowCurrent.id == null ? "../workflow/current/save" : "../workflow/current/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.workflowCurrent),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../workflow/current/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.workflowCurrent = r.workflowCurrent;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#workflowCurrentGrid").jqGrid('getGridParam', 'page');
			$("#workflowCurrentGrid").jqGrid('setGridParam', {
                postData: {'businessName': vm.q.businessName},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                businessName: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});